<!DOCTYPE html>
<html lang="ja">
<head>
    <meta http-equiv="content-type" content="text/html;charset=utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
    <meta name="description" content="これにより、Atariのゲームがマルチプロセッシングで実装されます。"/>

    <meta name="twitter:card" content="summary"/>
    <meta name="twitter:image:src" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta name="twitter:title" content="マルチプロセッシング機能を備えた Atari ラッパー"/>
    <meta name="twitter:description" content="これにより、Atariのゲームがマルチプロセッシングで実装されます。"/>
    <meta name="twitter:site" content="@labmlai"/>
    <meta name="twitter:creator" content="@labmlai"/>

    <meta property="og:url" content="https://nn.labml.ai/rl/game.html"/>
    <meta property="og:title" content="マルチプロセッシング機能を備えた Atari ラッパー"/>
    <meta property="og:image" content="https://avatars1.githubusercontent.com/u/64068543?s=400&amp;v=4"/>
    <meta property="og:site_name" content="マルチプロセッシング機能を備えた Atari ラッパー"/>
    <meta property="og:type" content="object"/>
    <meta property="og:title" content="マルチプロセッシング機能を備えた Atari ラッパー"/>
    <meta property="og:description" content="これにより、Atariのゲームがマルチプロセッシングで実装されます。"/>

    <title>マルチプロセッシング機能を備えた Atari ラッパー</title>
    <link rel="shortcut icon" href="/icon.png"/>
    <link rel="stylesheet" href="../pylit.css?v=1">
    <link rel="canonical" href="https://nn.labml.ai/rl/game.html"/>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/katex@0.13.18/dist/katex.min.css" integrity="sha384-zTROYFVGOfTw7JV7KUu8udsvW2fx4lWOsCEDqhBreBwlHI4ioVRtmIvEThzJHGET" crossorigin="anonymous">

    <!-- Global site tag (gtag.js) - Google Analytics -->
    <script async src="https://www.googletagmanager.com/gtag/js?id=G-4V3HC8HBLH"></script>
    <script>
        window.dataLayer = window.dataLayer || [];

        function gtag() {
            dataLayer.push(arguments);
        }

        gtag('js', new Date());

        gtag('config', 'G-4V3HC8HBLH');
    </script>
</head>
<body>
<div id='container'>
    <div id="background"></div>
    <div class='section'>
        <div class='docs'>
            <p>
                <a class="parent" href="/">home</a>
                <a class="parent" href="index.html">rl</a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations" target="_blank">
                    <img alt="Github"
                         src="https://img.shields.io/github/stars/labmlai/annotated_deep_learning_paper_implementations?style=social"
                         style="max-width:100%;"/></a>
                <a href="https://twitter.com/labmlai" rel="nofollow" target="_blank">
                    <img alt="Twitter"
                         src="https://img.shields.io/twitter/follow/labmlai?style=social"
                         style="max-width:100%;"/></a>
            </p>
            <p>
                <a href="https://github.com/labmlai/annotated_deep_learning_paper_implementations/tree/master/labml_nn/rl/game.py" target="_blank">
                    View code on Github</a>
            </p>
        </div>
    </div>
    <div class='section' id='section-0'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-0'>#</a>
            </div>
            <h1>マルチプロセッシング機能を備えた Atari ラッパー</h1>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">9</span><span></span><span class="kn">import</span> <span class="nn">multiprocessing</span>
<span class="lineno">10</span><span class="kn">import</span> <span class="nn">multiprocessing.connection</span>
<span class="lineno">11</span>
<span class="lineno">12</span><span class="kn">import</span> <span class="nn">cv2</span>
<span class="lineno">13</span><span class="kn">import</span> <span class="nn">gym</span>
<span class="lineno">14</span><span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-1'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-1'>#</a>
            </div>
            <p><a id="GameEnvironment"></a></p>
<h2>ゲーム環境</h2>
<p>これはOpenAIジムゲーム環境のラッパーです。ここではいくつかのことを行います。</p>
<p>1。4 つのフレームに同じアクションを適用し、最後のフレーム 2 を取得します。観測フレームをグレーに変換し、(84、84) 3 にスケーリングします。最後の4つのアクションを4フレーム重ねる 4.モニタリング用のエピソード情報 (エピソード全体の報酬総額) を追加 5.エピソードを1つのライフに制限します（ゲームにはライフが5つあり、ライフが1つ増えるたびにリセットされます</p>）
<h4>観測フォーマット</h4>
<p>観測値はサイズ (4, 84, 84) のテンソルです。最初の軸に積み重ねられた4つのフレーム（ゲーム画面の画像）です。つまり、各チャンネルはフレームです</p>。

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">17</span><span class="k">class</span> <span class="nc">Game</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-2'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-2'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">38</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seed</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-3'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-3'>#</a>
            </div>
            <p>環境を作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">40</span>        <span class="bp">self</span><span class="o">.</span><span class="n">env</span> <span class="o">=</span> <span class="n">gym</span><span class="o">.</span><span class="n">make</span><span class="p">(</span><span class="s1">&#39;BreakoutNoFrameskip-v4&#39;</span><span class="p">)</span>
<span class="lineno">41</span>        <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">seed</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-4'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-4'>#</a>
            </div>
            <p>4フレームのスタックのテンソル</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">44</span>        <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">4</span><span class="p">,</span> <span class="mi">84</span><span class="p">,</span> <span class="mi">84</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-5'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-5'>#</a>
            </div>
            <p>最後の 2 フレームまで保存するバッファ</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">47</span>        <span class="bp">self</span><span class="o">.</span><span class="n">obs_2_max</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">zeros</span><span class="p">((</span><span class="mi">2</span><span class="p">,</span> <span class="mi">84</span><span class="p">,</span> <span class="mi">84</span><span class="p">))</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-6'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-6'>#</a>
            </div>
            <p>エピソードの報酬を把握しておけ</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">50</span>        <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span> <span class="o">=</span> <span class="p">[]</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-7'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-7'>#</a>
            </div>
            <p>そして残された命の数</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">52</span>        <span class="bp">self</span><span class="o">.</span><span class="n">lives</span> <span class="o">=</span> <span class="mi">0</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-8'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-8'>#</a>
            </div>
            <h3>ステップ</h3>
<p><code  class="highlight"><span></span><span class="n">action</span></code>
4つのタイムステップを実行し、(観測、報酬、完了、エピソード情報) のタプルを返します。</p>
<ul><li><code  class="highlight"><span></span><span class="n">observation</span></code>
: 4 つのフレームを積み重ねた (このフレームと最後の 3 アクションのフレーム)</li>
<li><code  class="highlight"><span></span><span class="n">reward</span></code>
: アクション実行中の報酬の合計</li>
<li><code  class="highlight"><span></span><span class="n">done</span></code>
: エピソードが終わったかどうか (命が失われた)</li>
</ul><li><code  class="highlight"><span></span><span class="n">episode_info</span></code>
: エピソード情報 (完了した場合)</li>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">54</span>    <span class="k">def</span> <span class="nf">step</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">action</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-9'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-9'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">66</span>        <span class="n">reward</span> <span class="o">=</span> <span class="mf">0.</span>
<span class="lineno">67</span>        <span class="n">done</span> <span class="o">=</span> <span class="kc">None</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-10'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-10'>#</a>
            </div>
            <p>4 ステップ実行</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">70</span>        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-11'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-11'>#</a>
            </div>
            <p>OpenAI ジム環境でアクションを実行する</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">72</span>            <span class="n">obs</span><span class="p">,</span> <span class="n">r</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">info</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">action</span><span class="p">)</span>
<span class="lineno">73</span>
<span class="lineno">74</span>            <span class="k">if</span> <span class="n">i</span> <span class="o">&gt;=</span> <span class="mi">2</span><span class="p">:</span>
<span class="lineno">75</span>                <span class="bp">self</span><span class="o">.</span><span class="n">obs_2_max</span><span class="p">[</span><span class="n">i</span> <span class="o">%</span> <span class="mi">2</span><span class="p">]</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_obs</span><span class="p">(</span><span class="n">obs</span><span class="p">)</span>
<span class="lineno">76</span>
<span class="lineno">77</span>            <span class="n">reward</span> <span class="o">+=</span> <span class="n">r</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-12'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-12'>#</a>
            </div>
            <p>残りライフ数を取得</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">80</span>            <span class="n">lives</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">unwrapped</span><span class="o">.</span><span class="n">ale</span><span class="o">.</span><span class="n">lives</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-13'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-13'>#</a>
            </div>
            <p>命が失われたらリセット</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">82</span>            <span class="k">if</span> <span class="n">lives</span> <span class="o">&lt;</span> <span class="bp">self</span><span class="o">.</span><span class="n">lives</span><span class="p">:</span>
<span class="lineno">83</span>                <span class="n">done</span> <span class="o">=</span> <span class="kc">True</span>
<span class="lineno">84</span>                <span class="k">break</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-14'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-14'>#</a>
            </div>
            <p>各ステップの報酬を維持</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">87</span>        <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="o">.</span><span class="n">append</span><span class="p">(</span><span class="n">reward</span><span class="p">)</span>
<span class="lineno">88</span>
<span class="lineno">89</span>        <span class="k">if</span> <span class="n">done</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-15'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-15'>#</a>
            </div>
            <p>終了したら、エピソードが終了したらエピソード情報を設定し、リセットします</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">91</span>            <span class="n">episode_info</span> <span class="o">=</span> <span class="p">{</span><span class="s2">&quot;reward&quot;</span><span class="p">:</span> <span class="nb">sum</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="p">),</span> <span class="s2">&quot;length&quot;</span><span class="p">:</span> <span class="nb">len</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">rewards</span><span class="p">)}</span>
<span class="lineno">92</span>            <span class="bp">self</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span>
<span class="lineno">93</span>        <span class="k">else</span><span class="p">:</span>
<span class="lineno">94</span>            <span class="n">episode_info</span> <span class="o">=</span> <span class="kc">None</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-16'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-16'>#</a>
            </div>
            <p>最後の 2 フレームの最大値を取得</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">97</span>            <span class="n">obs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_2_max</span><span class="o">.</span><span class="n">max</span><span class="p">(</span><span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-17'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-17'>#</a>
            </div>
            <p>4フレームのスタックにプッシュ</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">100</span>            <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">roll</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">,</span> <span class="n">shift</span><span class="o">=-</span><span class="mi">1</span><span class="p">,</span> <span class="n">axis</span><span class="o">=</span><span class="mi">0</span><span class="p">)</span>
<span class="lineno">101</span>            <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">[</span><span class="o">-</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="n">obs</span>
<span class="lineno">102</span>
<span class="lineno">103</span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">,</span> <span class="n">reward</span><span class="p">,</span> <span class="n">done</span><span class="p">,</span> <span class="n">episode_info</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-18'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-18'>#</a>
            </div>
            <h3>環境をリセット</h3>
<p>エピソード情報と4フレームスタックのクリーンアップ</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">105</span>    <span class="k">def</span> <span class="nf">reset</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-19'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-19'>#</a>
            </div>
            <p>OpenAI ジム環境をリセット</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">112</span>        <span class="n">obs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">reset</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-20'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-20'>#</a>
            </div>
            <p>キャッシュをリセット</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">115</span>        <span class="n">obs</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">_process_obs</span><span class="p">(</span><span class="n">obs</span><span class="p">)</span>
<span class="lineno">116</span>        <span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">4</span><span class="p">):</span>
<span class="lineno">117</span>            <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span><span class="p">[</span><span class="n">i</span><span class="p">]</span> <span class="o">=</span> <span class="n">obs</span>
<span class="lineno">118</span>        <span class="bp">self</span><span class="o">.</span><span class="n">rewards</span> <span class="o">=</span> <span class="p">[]</span>
<span class="lineno">119</span>
<span class="lineno">120</span>        <span class="bp">self</span><span class="o">.</span><span class="n">lives</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">env</span><span class="o">.</span><span class="n">unwrapped</span><span class="o">.</span><span class="n">ale</span><span class="o">.</span><span class="n">lives</span><span class="p">()</span>
<span class="lineno">121</span>
<span class="lineno">122</span>        <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">obs_4</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-21'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-21'>#</a>
            </div>
            <h4>ゲームフレームの処理</h4>
<p>ゲームフレームをグレーに変換し、84x84に再スケーリング</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">124</span>    <span class="nd">@staticmethod</span>
<span class="lineno">125</span>    <span class="k">def</span> <span class="nf">_process_obs</span><span class="p">(</span><span class="n">obs</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-22'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-22'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">130</span>        <span class="n">obs</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">cvtColor</span><span class="p">(</span><span class="n">obs</span><span class="p">,</span> <span class="n">cv2</span><span class="o">.</span><span class="n">COLOR_RGB2GRAY</span><span class="p">)</span>
<span class="lineno">131</span>        <span class="n">obs</span> <span class="o">=</span> <span class="n">cv2</span><span class="o">.</span><span class="n">resize</span><span class="p">(</span><span class="n">obs</span><span class="p">,</span> <span class="p">(</span><span class="mi">84</span><span class="p">,</span> <span class="mi">84</span><span class="p">),</span> <span class="n">interpolation</span><span class="o">=</span><span class="n">cv2</span><span class="o">.</span><span class="n">INTER_AREA</span><span class="p">)</span>
<span class="lineno">132</span>        <span class="k">return</span> <span class="n">obs</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-23'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-23'>#</a>
            </div>
            <h2>ワーカープロセス</h2>
<p>各ワーカープロセスはこのメソッドを実行します</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">135</span><span class="k">def</span> <span class="nf">worker_process</span><span class="p">(</span><span class="n">remote</span><span class="p">:</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">connection</span><span class="o">.</span><span class="n">Connection</span><span class="p">,</span> <span class="n">seed</span><span class="p">:</span> <span class="nb">int</span><span class="p">):</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-24'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-24'>#</a>
            </div>
            <p>ゲーム作成</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">143</span>    <span class="n">game</span> <span class="o">=</span> <span class="n">Game</span><span class="p">(</span><span class="n">seed</span><span class="p">)</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-25'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-25'>#</a>
            </div>
            <p>接続からの指示を待って実行する</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">146</span>    <span class="k">while</span> <span class="kc">True</span><span class="p">:</span>
<span class="lineno">147</span>        <span class="n">cmd</span><span class="p">,</span> <span class="n">data</span> <span class="o">=</span> <span class="n">remote</span><span class="o">.</span><span class="n">recv</span><span class="p">()</span>
<span class="lineno">148</span>        <span class="k">if</span> <span class="n">cmd</span> <span class="o">==</span> <span class="s2">&quot;step&quot;</span><span class="p">:</span>
<span class="lineno">149</span>            <span class="n">remote</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">game</span><span class="o">.</span><span class="n">step</span><span class="p">(</span><span class="n">data</span><span class="p">))</span>
<span class="lineno">150</span>        <span class="k">elif</span> <span class="n">cmd</span> <span class="o">==</span> <span class="s2">&quot;reset&quot;</span><span class="p">:</span>
<span class="lineno">151</span>            <span class="n">remote</span><span class="o">.</span><span class="n">send</span><span class="p">(</span><span class="n">game</span><span class="o">.</span><span class="n">reset</span><span class="p">())</span>
<span class="lineno">152</span>        <span class="k">elif</span> <span class="n">cmd</span> <span class="o">==</span> <span class="s2">&quot;close&quot;</span><span class="p">:</span>
<span class="lineno">153</span>            <span class="n">remote</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="lineno">154</span>            <span class="k">break</span>
<span class="lineno">155</span>        <span class="k">else</span><span class="p">:</span>
<span class="lineno">156</span>            <span class="k">raise</span> <span class="ne">NotImplementedError</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-26'>
        <div class='docs doc-strings'>
            <div class='section-link'>
                <a href='#section-26'>#</a>
            </div>
            <p>新しいワーカーを作成し、別のプロセスで実行します。</p>

        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">159</span><span class="k">class</span> <span class="nc">Worker</span><span class="p">:</span></pre></div>
        </div>
    </div>
    <div class='section' id='section-27'>
        <div class='docs'>
            <div class='section-link'>
                <a href='#section-27'>#</a>
            </div>
            
        </div>
        <div class='code'>
            <div class="highlight"><pre><span class="lineno">164</span>    <span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">seed</span><span class="p">):</span>
<span class="lineno">165</span>        <span class="bp">self</span><span class="o">.</span><span class="n">child</span><span class="p">,</span> <span class="n">parent</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Pipe</span><span class="p">()</span>
<span class="lineno">166</span>        <span class="bp">self</span><span class="o">.</span><span class="n">process</span> <span class="o">=</span> <span class="n">multiprocessing</span><span class="o">.</span><span class="n">Process</span><span class="p">(</span><span class="n">target</span><span class="o">=</span><span class="n">worker_process</span><span class="p">,</span> <span class="n">args</span><span class="o">=</span><span class="p">(</span><span class="n">parent</span><span class="p">,</span> <span class="n">seed</span><span class="p">))</span>
<span class="lineno">167</span>        <span class="bp">self</span><span class="o">.</span><span class="n">process</span><span class="o">.</span><span class="n">start</span><span class="p">()</span></pre></div>
        </div>
    </div>
    <div class='footer'>
        <a href="https://papers.labml.ai">Trending Research Papers</a>
        <a href="https://labml.ai">labml.ai</a>
    </div>
</div>
<script src=../interactive.js?v=1"></script>
<script>
    function handleImages() {
        var images = document.querySelectorAll('p>img')

        for (var i = 0; i < images.length; ++i) {
            handleImage(images[i])
        }
    }

    function handleImage(img) {
        img.parentElement.style.textAlign = 'center'

        var modal = document.createElement('div')
        modal.id = 'modal'

        var modalContent = document.createElement('div')
        modal.appendChild(modalContent)

        var modalImage = document.createElement('img')
        modalContent.appendChild(modalImage)

        var span = document.createElement('span')
        span.classList.add('close')
        span.textContent = 'x'
        modal.appendChild(span)

        img.onclick = function () {
            console.log('clicked')
            document.body.appendChild(modal)
            modalImage.src = img.src
        }

        span.onclick = function () {
            document.body.removeChild(modal)
        }
    }

    handleImages()
</script>
</body>
</html>